package com.health.userbackstage.utils;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtils {

    private ThreadPoolUtils() {}

    /**
     * ThreadPoolExecutor(
     *                    int corePoolSize,
     *                    int maximumPoolSize,
     *                    long keepAliveTime,
     *                    TimeUnit unit,
     *                    BlockingQueue<Runnable> workQueue,
     *                    ThreadFactory threadFactory,
     *                    RejectedExecutionHandler handler
     *                    )
     * 参数1 corePoolSize 核心线程数
     * 参数2 maximumPoolSize 最大线程数
     * 参数3 keepAliveTime 空闲线程销毁时间
     * 参数4 unit 空闲线程销毁的时间单位
     * 参数5 workQueue 缓存队列
     * 参数6 threadFactory 线程工厂
     * 参数7 handler 拒绝策略
     *
     * 线程池的线程数如何取值
     *      io密集型：（文件，数据库操作） cpu核数+1
     *      cpu密集型：（普通计算） 2*cpu核数
     *
     */
    private static ThreadPoolExecutor threadPoolExecutor =
            new ThreadPoolExecutor(5,
                    5,
                    0,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.AbortPolicy()
            );

    //懒汉式
    public static ThreadPoolExecutor threadPoolExecutor(){
        return threadPoolExecutor;
    }
}
